package com.supplychainapi.controller;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.joindata.inf.common.util.basic.JsonUtil;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.model.ScmTbstorage;
import com.supplychainapi.service.IScmTbstorageService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * 手机版仓库
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-10-11
 *
 */
@RequestMapping(PathUitls.ROOT_PHONE+PathUitls.ROOT_VERSION+PathUitls.ROOT_WAREHOUSE)
@RestController
@Api(value = "WarehousePhoneController", description = "手机版仓库")
public class WarehouseController extends BaseController {

	private Logger logger = LogManager.getLogger(WarehouseController.class);

	@Autowired
	private IScmTbstorageService scmTbstoragePhoneService;

	@ApiVersion(1)
	@ApiOperation("新增仓库")
	@RequestMapping(value = "/addStorageInfo", method=RequestMethod.POST)
	public RestResponse<ScmTbstorage> addStorageInfo(@RequestBody ScmTbstorage storage,@ApiIgnore LoginCache loginCache,HttpServletRequest request){

		logger.info("参数信息 ----- storage={}", storage == null ? null : JsonUtil.toJSON(storage,false));

		storage.setFsShopGUID(loginCache.getFsShopGUID());
		if (StringUtils.isEmpty(storage.getFsStorageId())) {
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "仓库代码不能为空");
		}

		if (StringUtils.isEmpty(storage.getFsShopGUID())) {
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "店铺GUID不能为空");
		}

		if (StringUtils.isEmpty(storage.getFsStorageName()) || storage.getFsStorageName().trim().equals("")) {
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "仓库名称不能为空");
		}
		storage.setFsCreateUserId(loginCache.getFsUserId());
		storage.setFsCreateUserName(loginCache.getFsUserName());
		storage.setFsUpdateUserId(loginCache.getFsUserId());
		storage.setFsUpdateUserName(loginCache.getFsUserName());
		try {

			Boolean bl = scmTbstoragePhoneService.getStorageDeDuplication(storage.getFsShopGUID(),storage.getFsStorageName(),storage.getFsStorageId(),"add");
			if(bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.STORAGE_ERROR_MESSAGE.getNum(), "当前仓库名称已存在");
			}
			scmTbstoragePhoneService.addScmTbstorageInfo(storage);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(),e.getMessage());
		}
		return RestResponse.success();

	}

	@ApiVersion(1)
	@ApiOperation("查询仓库列表信息")
	@RequestMapping(value = "/getStorageListInfo", method=RequestMethod.GET)
	public RestResponse<List<ScmTbstorage>> getStorageListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "页码", required = true) @RequestParam(required = true) Integer page, @ApiParam(value = "数据大小", required = true) @RequestParam(required = true) Integer pageSize, HttpServletRequest request) {
		logger.info("查询仓库列表 参数 fsShopGUID={},page={},pageSize={}", loginCache.getFsShopGUID(), page, pageSize);
		List<ScmTbstorage> scmTbstorageList = null;
		try {
			scmTbstorageList = scmTbstoragePhoneService.getScmTbstorageListInfo(loginCache.getFsShopGUID(), page, pageSize);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(scmTbstorageList);
	}

	@ApiVersion(1)
	@ApiOperation("仓库列表条件模糊查询")
	@RequestMapping(value = "/getStorageLikeListInfo", method = RequestMethod.GET)
	public RestResponse<List<ScmTbstorage>> getStorageLikeListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "仓库名称", required = true) @RequestParam String fsStorageName, @ApiParam(value = "页码", required = true) @RequestParam(required = true) Integer page, @ApiParam(value = "数据大小", required = true) @RequestParam(required = true) Integer pageSize, HttpServletRequest request) {
        logger.info("查询仓库列表 参数 fsShopGUID={},fsStorageName={},page={},pageSize={}", loginCache.getFsShopGUID(), fsStorageName, page, pageSize);
        List<ScmTbstorage> scmTbstorageList = null;
        try {
            scmTbstorageList = scmTbstoragePhoneService.getScmTbstorageLikeListInfo(loginCache.getFsShopGUID(), fsStorageName, page, pageSize);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(scmTbstorageList);
	}

	@ApiVersion(1)
	@ApiOperation("删除仓库信息")
	@RequestMapping(value = "/deleteStorageInfo", method = RequestMethod.GET)
	public RestResponse<ScmTbstorage> deleteStorageInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "仓库代码", required = true) @RequestParam(required = true) String fsStorageId, HttpServletRequest request) {
		logger.info("查询仓库列表 参数 fsShopGUID={},fsStorageId={}", loginCache.getFsShopGUID(), fsStorageId);
		if (StringUtils.isEmpty(fsStorageId)) {
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "仓库代码不能为空");
		}
		try {
			scmTbstoragePhoneService.deleteStorageInfo(loginCache.getFsShopGUID(), fsStorageId);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("修改仓库信息")
	@RequestMapping(value = "/updateStorageInfo", method = RequestMethod.POST)
	public RestResponse<ScmTbstorage> updateStorageInfo(@RequestBody ScmTbstorage storage, @ApiIgnore LoginCache loginCache, HttpServletRequest request) {
        logger.info("修改仓库参数信息 ----- storage={}", storage == null ? null : JsonUtil.toJSON(storage, false));
        storage.setFsShopGUID(loginCache.getFsShopGUID());
        if (loginCache.getFsShopGUID() == null) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "店铺GUID不能为空");
		}

		if (storage.getFsStorageId() == null) {
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "仓库代码不能为空");
		}
		if(storage.getFsStorageName()==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "仓库名称不能为空");
		}
		storage.setFsShopGUID(loginCache.getFsShopGUID());
		storage.setFsUpdateUserId(loginCache.getFsUserId());
		storage.setFsUpdateUserName(loginCache.getFsUserName());
		storage.setFsUpdateTime(new Date());
		try {
			Boolean bl = scmTbstoragePhoneService.getStorageDeDuplication(storage.getFsShopGUID(),storage.getFsStorageName(),storage.getFsStorageId(),"update");
			if(bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.STORAGE_ERROR_MESSAGE.getNum(), "当前仓库名称已存在");
			}
			scmTbstoragePhoneService.updateStorageInfo(storage);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success();
	}

}
